/*
* Copyright (C) 2012 Manuel Pérez
*
* This file is part of OpenSudoku.
*
* OpenSudoku is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* OpenSudoku is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with OpenSudoku. If not, see <http://www.gnu.org/licenses/>.
*
*/
package igraf.bezeroa;

import igraf.ItzuliInterfazea;
import igraf.Panela;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.SystemColor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

import dBasea.Bezeroa;

public class BezeroBerrizketa extends Panela{

	
	private static final long serialVersionUID = -3929109096376189486L;
	private JButton ok;
	private JButton ezeztatu;
	private JLabel zenbakia;
	private JTextField nif;
	private JTextField matrikula;
	private JLabel matri;
	private JRadioButton bai;
	private JRadioButton ez;
	private ButtonGroup baiez;
    private Bezeroa beze=new Bezeroa();


	public BezeroBerrizketa(String tituloa, final ItzuliInterfazea itzuliInterfazea) {		
		super(tituloa);
		
		JPanel erdikoPanela = this.getErdikoPanela();

		erdikoPanela.setBackground(SystemColor.activeCaption);
		erdikoPanela.setLayout(new BorderLayout());

		JPanel erdikoa = new JPanel();
		erdikoa.setBackground(SystemColor.activeCaption);		
		erdikoPanela.add(erdikoa, BorderLayout.CENTER);
		erdikoa.setLayout(new GridLayout(7,1));
		
		JPanel ge=new JPanel();
		ge.setBackground(SystemColor.activeCaption);
		erdikoa.add(ge);
		
		JPanel g=new JPanel();
		g.setBackground(SystemColor.activeCaption);
		erdikoa.add(g);
		
		JPanel arri = new JPanel();
		arri.setBackground(SystemColor.activeCaption);
		FlowLayout fl_arri = new FlowLayout();
		fl_arri.setHgap(15);
		fl_arri.setVgap(10);
		arri.setLayout(fl_arri);
		zenbakia=new JLabel("Bezero NIF:");
		zenbakia.setFont(new Font("Arial", Font.PLAIN, 14));
		nif=new JTextField(8);
		nif.addKeyListener(new KeyAdapter() {
			
			public void keyTyped(KeyEvent arg0) {
				if (nif.getText().length()>=10){
					arg0.consume();
					}
			}
		});
		arri.add(zenbakia);
		arri.add(nif);
		erdikoa.add(arri);
		
		JPanel arribb = new JPanel();
		arribb.setBackground(SystemColor.activeCaption);
		arri.setBackground(SystemColor.activeCaption);
		FlowLayout arrib = new FlowLayout();
		arrib.setHgap(15);
		arrib.setVgap(10);
		arribb.setLayout(arrib);
		matrikula=new JTextField(15);
		matri=new JLabel("Kotxe matrikula:");
		arribb.add(matri);
		matri.setFont(new Font("Arial", Font.PLAIN, 14));
		arribb.add(matrikula);
		erdikoa.add(arribb);
		
		JPanel gs=new JPanel();
		gs.setBackground(SystemColor.activeCaption);
		erdikoa.add(gs);
		
		try {
			List<String> aseguruTipoak = beze.getAseguruTipoDeskripzioa();
			bai=new JRadioButton(aseguruTipoak.get(0), true);
			bai.setFont(new Font("Arial", Font.PLAIN, 14));
			ez=new JRadioButton(aseguruTipoak.get(1));
			ez.setFont(new Font("Arial", Font.PLAIN, 14));
			baiez= new ButtonGroup();
			baiez.add(bai);
			baiez.add(ez);
			gs.add(bai);
			gs.add(ez);
		}
		catch (SQLException e1) {
			e1.printStackTrace();
		}
		JPanel xxx = new JPanel();
		xxx.setBackground(SystemColor.activeCaption);
		erdikoa.add(xxx);
		FlowLayout fl_behekoa = new FlowLayout();
		fl_behekoa.setVgap(20);
		fl_behekoa.setHgap(110);
		JPanel behekoa = new JPanel(fl_behekoa);
		behekoa.setBackground(SystemColor.activeCaption);
		ok = new JButton("Berriztatu");
		ok.setHorizontalAlignment(SwingConstants.LEFT);
		ok.setFont(new Font("Arial", Font.PLAIN, 14));
		behekoa.add(ok);
		ezeztatu = new JButton("Ezeztatu");
		ezeztatu.setHorizontalAlignment(SwingConstants.RIGHT);
		ezeztatu.setFont(new Font("Arial", Font.PLAIN, 14));
		behekoa.add(ezeztatu);
		erdikoPanela.add(behekoa, BorderLayout.SOUTH);
		
		ok.addActionListener(new ActionListener() {
			@SuppressWarnings("deprecation")
			public void actionPerformed(ActionEvent arg0) {
				try {
					if (!zerbaiIdatzi()) {
						JOptionPane.showMessageDialog(null,
								"Sartu behar duzu zerbait zuriunetan\n"
										+ "Ezin da eremu hutsik egon", "Oker",
								JOptionPane.ERROR_MESSAGE);
					} 
					else if (!beze.aseguruaExistitzenDa(nif.getText(), matrikula.getText())) {
						JOptionPane.showMessageDialog(null,
								"Bezero hau ez dago erlazionatuta matrikula honekin",
								"Oker", JOptionPane.ERROR_MESSAGE);
					}
					else {
						String balioa = "";
							if (bai.isSelected()) {
								balioa = bai.getText();
							}
							else if (ez.isSelected()) {
								balioa = ez.getText();
						}// ORAIN DELA ASKO EZ DA GURE BEZEROA( Gaur + 1year)
						long ur = beze.urteaLortu(nif.getText(),
								matrikula.getText());
						Date date = new Date(ur);
						Date gaur = new Date();
						if (gaur.getYear() - date.getYear() >= 2) {
							Date deit = beze.bezBerriztu2(matrikula.getText(),
									nif.getText(), balioa);
							int fech = deit.getYear() + 1900;
							JOptionPane.showMessageDialog(null,
									"Ondo berriztuta. Data limitea: " + fech,
									"", JOptionPane.INFORMATION_MESSAGE);
							eremuakGarbitu();
						} else {

							// URTE BAT BERRIZTATU (Year+1)
							Date data = beze.bezBerriztu(matrikula.getText(),
									nif.getText(), balioa);
							int fech = data.getYear() + 1900;
							JOptionPane.showMessageDialog(null,
									"Ondo berriztuta. Data limitea: " + fech,
									"", JOptionPane.INFORMATION_MESSAGE);
							eremuakGarbitu();
						}
					}
				} catch (Exception e) {
					JOptionPane.showMessageDialog(null,
							"Ez da aurkitu, sartu bat",
							"Oker", JOptionPane.ERROR_MESSAGE);
					e.printStackTrace();
				}
			}
		});
		
		ezeztatu.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				itzuliInterfazea.itzuliLehenPanela();
			}
		});
	
	}


	private boolean zerbaiIdatzi() {
		boolean aurki;
		aurki=nif.getText().length() > 0 && matrikula.getText().length() > 0 ;
   		return aurki;
   	}
	public void eremuakGarbitu() {
		nif.setText("");
		matrikula.setText("");
		bai.setSelected(true);
		
	}

}
